package com.gmzg.gmzgnews.me.view;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.gmzg.gmzgnews.MainApplication;
import com.gmzg.gmzgnews.R;
import com.squareup.picasso.Picasso;

import org.xutils.DbManager;
import org.xutils.x;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.smssdk.EventHandler;
import cn.smssdk.SMSSDK;
import me.iwf.photopicker.PhotoPickerActivity;
import me.iwf.photopicker.utils.PhotoPickerIntent;

import static com.gmzg.gmzgnews.me.view.MeFragment.REQUEST_CODE;

public class RegisterActivity extends AppCompatActivity {


    private static final String TAG = RegisterActivity.class.getSimpleName();
    @BindView(R.id.register_toolbar)
    Toolbar mRegisterToolbar;
    @BindView(R.id.image)
    CircleImageView mImage;
    @BindView(R.id.register_nickname)
    EditText mRegisterNickname;
    @BindView(R.id.register_phonenumber)
    EditText mRegisterPhonenumber;
    @BindView(R.id.register_btn)
    Button mRegisterBtn;
    @BindView(R.id.upload_image_tv)
    TextView mUploadImageTv;
    private ArrayList<String> photos;
    private String phoneNumber;
    private DbManager db;
    private EventHandler mEventHandler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        ButterKnife.bind(this);
        setSupportActionBar(mRegisterToolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        SMSSDK.initSDK(this,"18b5714872042","0ac563367fe05f4815de4921ea1ec19b");

        //获取操作本地数据库的db
        db = ((MainApplication) getApplication()).getDb();
        //获取手机号码
        TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        phoneNumber = telephonyManager.getLine1Number();
        if (phoneNumber != null) {
            mRegisterPhonenumber.setText(phoneNumber);
        }


        mEventHandler = new EventHandler() {
            @Override
            public void afterEvent(int event, int result, Object data) {
                if (result == SMSSDK.RESULT_COMPLETE) {//回调完成
                    switch (event) {
                        case SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE://提交验证码成功
                            break;
                        case SMSSDK.EVENT_GET_VERIFICATION_CODE://获取验证码成功

                            break;
                        case SMSSDK.EVENT_GET_SUPPORTED_COUNTRIES://返回支持发送验证码的国家列表
                            ArrayList<HashMap<String, Object>> data1 = (ArrayList<HashMap<String, Object>>) data;
                            for (HashMap<String, Object> map : data1) {
                                Iterator<String> iterator = map.keySet().iterator();
                                Log.d(TAG, "afterEvent: 开始");
                                while (iterator.hasNext()){
                                    String key = iterator.next();
                                    Object value = map.get(key);
                                    Log.d(TAG, "afterEvent: "+"key:"+key+"value:"+value);
                                }
                                Log.d(TAG, "afterEvent: 完成");
                            }
                            Log.d(TAG, "afterEvent: ");
                            break;
                    }
                }else {
                    ((Throwable) data).printStackTrace();
                }
            }
        };
        //注册短信回调
        SMSSDK.registerEventHandler(mEventHandler);
    }

    @OnClick({R.id.upload_image_tv, R.id.register_btn})
    public void onClick(View view) {
        int id = view.getId();
        switch (id) {
            case R.id.upload_image_tv:
                PhotoPickerIntent intent = new PhotoPickerIntent(this);
                intent.setPhotoCount(1);
                intent.setShowCamera(true);
                startActivityForResult(intent, REQUEST_CODE);
                break;
            case R.id.register_btn:
                final String nickname = mRegisterNickname.getText().toString().trim();
                final String phoneNumber = mRegisterPhonenumber.getText().toString().trim();
                if (!TextUtils.isEmpty(nickname) && !TextUtils.isEmpty(phoneNumber)) {
                    //发送验证码到该手机号
                    new AlertDialog.Builder(this)
                            .setTitle("确认手机号码")
                            .setMessage("我们将发送验证码短信到这个手机号码：\n" + phoneNumber)
                            .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    Toast.makeText(RegisterActivity.this, "点击了确认", Toast.LENGTH_SHORT).show();
                                    Intent intent1 = new Intent(RegisterActivity.this, VerificateCodeActivity.class);
                                    Bundle bundle = new Bundle();
                                    bundle.putString("username",nickname);
                                    bundle.putString("image",photos.get(0));
                                    bundle.putString("phoneNumber",phoneNumber);
                                    intent1.putExtra("bundle",bundle);
                                    startActivity(intent1);
                                    //点击确定的时候，需要将该页面的昵称，手机号和密码  nickname、phoneNumber、photos.get(0)
                                    //获取短信目前支持的国家列表
//                                    SMSSDK.getSupportedCountries();
                                }
                            })
                            .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    Toast.makeText(RegisterActivity.this, "点击了取消", Toast.LENGTH_SHORT).show();
                                }
                            })
                            .show();
                }else {
                    Toast.makeText(this, "昵称或手机号不能为空", Toast.LENGTH_SHORT).show();
                }
                break;
        }

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
            if (data != null) {
                photos = data.getStringArrayListExtra(PhotoPickerActivity.KEY_SELECTED_PHOTOS);
                //加载单张图片   /storage/emulated/0/Pictures/JPEG_20161103_224059.jpg
                Uri uri = Uri.fromFile(new File(photos.get(0)));
                mUploadImageTv.setVisibility(View.GONE);
                Picasso.with(this)
                        .load(uri)
                        .resize(200,200)
                        .into(mImage);
//                Glide.with(this)
//                        .load(uri)
//                        .centerCrop()
//                        .thumbnail(0.1f)
//                        .into(mImage);
            }
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        SMSSDK.unregisterEventHandler(mEventHandler);
    }
}
